package myServlet;

import Smt.ServerImp.SmtUserServerImp;
import Smt.Util.SmtUser;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@SuppressWarnings("serial")
public class SmtUserAdd extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		int id = 0;
		String sex = request.getParameter("isex");
		String tel = request.getParameter("itel");
		String ages = request.getParameter("iage");
		int age = 0;
		if (ages != null) {
			age = Integer.parseInt(ages);
		}
		String password = request.getParameter("ipassword");
		String add = request.getParameter("iadd");
		String rms = request.getParameter("irms");
		String name = request.getParameter("iname");
		SmtUser s = new SmtUser(id, name, sex, age, tel, add, rms, password);
		SmtUserServerImp susi = new SmtUserServerImp();
		if (susi.add(s)) {
			response.sendRedirect("SmtUserRead");
		} else {
			request.setAttribute("error", "用户名已存在，添加失败！");
			request.getRequestDispatcher("SmtUserRead").forward(request, response);
		}

	}

}
